package com.lcd.ssm.web.controller;

import com.lcd.ssm.domain.Role;
import com.lcd.ssm.page.PageResult;
import com.lcd.ssm.query.RoleQueryObject;
import com.lcd.ssm.service.IRoleService;
import com.lcd.ssm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @RequestMapping("/role")
    public String index(){
        return "role";
    }

    @RequestMapping("/role_save")
    @ResponseBody
    public AjaxResult insert(Role role){
        AjaxResult ajaxResult = null;
        try {
            roleService.insert(role);
            ajaxResult = new AjaxResult(true, "添加成功");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult = new AjaxResult("添加失败");
        }
        return ajaxResult;

    }

    @RequestMapping("/role_update")
    @ResponseBody
    public AjaxResult update(Role role){
        AjaxResult ajaxResult = null;
        try {
            roleService.updateByPrimaryKey(role);
            ajaxResult = new AjaxResult(true, "成功");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult = new AjaxResult("失败");
        }
        return ajaxResult;

    }

    @RequestMapping("/role_list")
    @ResponseBody
    public PageResult list(RoleQueryObject qo){
        return roleService.queryForPage(qo);

    }

    @RequestMapping("/role_queryForEmployee")
    @ResponseBody
    public List<Role> queryForEmployee(){
        return roleService.selectAll();
    }

}
